//
//  EPMEmotionKeyBoardView.m
//  WeiBoByOC
//
//  Created by 马继鵬 on 17/4/7.
//  Copyright © 2017年 7TH. All rights reserved.
//

#import "EPMEmotionKeyBoardView.h"
#import "EPMEmotionPageView.h"
#import "EPMEmotionBottomView.h"

@interface EPMEmotionKeyBoardView()<UICollectionViewDelegate>
@property (weak,nonatomic) EPMEmotionPageView* emotionPageView;
@property (weak,nonatomic) EPMEmotionBottomView* emotionBottomView;
@end

@implementation EPMEmotionKeyBoardView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]){
        [self setupUI];
    }
    return self;
}
#pragma
#pragma-设置UI
- (void)setupUI{
    
    [self addEmotionPageView];
    [self addEmotionBotomView];
    
    dispatch_barrier_async(dispatch_get_main_queue(), ^{
        [self.emotionPageView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:1] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    });
}

#pragma
#pragma-添加表情展示界面
- (void)addEmotionPageView{
    EPMEmotionPageView* emotionPageView = [[EPMEmotionPageView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, 216-35)];
    self.emotionPageView = emotionPageView;
    [self addSubview:emotionPageView];
    emotionPageView.delegate = self;
    
    
}
- (void)addEmotionBotomView{
    
    EPMEmotionBottomView* emotionBottomView = [[EPMEmotionBottomView alloc]init];
    self.emotionBottomView = emotionBottomView;
    [self addSubview:emotionBottomView];
    [emotionBottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.bottom.trailing.equalTo(self);
        make.top.equalTo(self.emotionPageView.mas_bottom);
    }];
    emotionBottomView.btnClickBlock = ^(UIButton* btn){
        
    NSIndexPath* index = [NSIndexPath indexPathForItem:0 inSection:btn.tag - 2000];
        [self.emotionPageView scrollToItemAtIndexPath:index atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    };
    
}
#pragma
#pragma-emotionPage的代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat centerX = scrollView.contentOffset.x + screenWidth/2;
    
    CGPoint point = CGPointMake(centerX, 0);
    
    NSIndexPath* indexPath = [self.emotionPageView indexPathForItemAtPoint:point];
    
    [self.emotionBottomView setCurrentBtn:indexPath.section];
    
    
}

#pragma
#pragma-刷新最近表情视图
- (void)reloadRecntEmotions{
    NSIndexPath* indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
    [self.emotionPageView reloadItemsAtIndexPaths:@[indexPath]];
}


@end


